package com.core.framework.tools.xmloprations.noe;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import javax.xml.namespace.QName;
import javax.xml.stream.XMLStreamException;

import com.core.framework.tools.xmloprations.error.LoadException;
import com.core.framework.tools.xmloprations.register.XmlRegister;
import com.core.framework.tools.xmloprations.stream.EndDocumentEvent;
import com.core.framework.tools.xmloprations.stream.StartElementEvent;
import com.core.framework.tools.xmloprations.stream.XmlStreamEvent;
import com.core.log.Log;

/**
* 
* @version : 2011-11-15
* @author 赵永春 (zyc@byshell.org)
*/
public class XmlRunToos {
    public static void runXml(ArrayList<Object> xmlArray) throws LoadException, IOException, XMLStreamException {
        XmlRegister register = getXmlRegister();
        register.setContext(xmlContextMap);
        for (Object xml : xmlArray)
            if (xml instanceof File)
                register.addSource((File) xml);
            else
                register.addSource(xml.toString());
        register.loadXml();
        Log.debug("OK!");
    };
    public static void runXml(String sourcePath) throws LoadException, IOException, XMLStreamException {
        XmlRegister register = getXmlRegister();
        //        register.loadRegister("register.xml");
        register.setContext(xmlContextMap);
        register.addSource(sourcePath);
        register.loadXml();
        Log.debug("OK!");
    }
    //
    //
    static HashMap<String, XmlContextBean> xmlContextMap = new HashMap<String, XmlContextBean>();
    public static void addXmlContext(String namespace, XmlContext xmlContext) {
        Log.debug("addXmlContext " + namespace);
        xmlContextMap.put(namespace, new XmlContextBean(xmlContext));
    }
    public static XmlRegister getXmlRegister() throws LoadException, IOException, XMLStreamException {
        return new XmlRegisterTemp();
    }
};
class XmlRegisterTemp extends XmlRegister {
    public XmlRegisterTemp() throws IOException, XMLStreamException, LoadException {
        super();
    }
    protected boolean isLoadDefault() {
        return true;
    }
    public synchronized void sendEvent(XmlStreamEvent e) throws XMLStreamException, IOException {
        if (e instanceof StartElementEvent == true) {
            //
        } else if (e instanceof EndDocumentEvent == true) {
            //
            for (XmlContextBean xmlContext : XmlRunToos.xmlContextMap.values())
                if (xmlContext.isInit == true)
                    xmlContext.xmlContext.after();
        } else {
            QName qn = e.getCurrentElement();
            if (qn != null && qn.getNamespaceURI() != null) {
                XmlContextBean bean = XmlRunToos.xmlContextMap.get(qn.getNamespaceURI());
                if (bean.isInit == false) {
                    bean.xmlContext.before();
                    bean.isInit = true;
                }
            }
        }
        super.sendEvent(e);
    }
}
class XmlContextBean {
    protected boolean isInit     = false;
    public XmlContext xmlContext = null;
    public XmlContextBean(XmlContext xmlContext) {
        this.xmlContext = xmlContext;
    }
}